<template>
  <div class="resumeItemContainer">
    <div class="resumeItemTitle">
      <div class="resumeItemTitleFlag"></div>
      <span class="resumeItemTitleContent">校园经历</span>
      <span class="resumeItemTitleAddIcon" @click="handleAddClick"><img :src="resumeItemAddIcon"
          class="resumeItemAddIcon"> 添加</span>
    </div>
    <div class="resumeItemMain">
      <div class="resumeItemDisplayList">
        <div class="resumeItemDisplayItem" v-for="campusExpItem,index in resumeDataList"
          :key="campusExpItem.person_studengLeaderId">
          <div class="resumeItemDisplayItemEditButtons">
            <template v-if="!campusExpItem.editStatus">
              <span class="resumeItemDisplayItemEditButton" @click="changeEditStatus(campusExpItem,true)">
                <img :src="myResumeEditIcon" /> 编辑</span>
              <span class="resumeItemDisplayItemEditButton" @click="handleDeletClick(campusExpItem)"
                v-if="resumeDataList.length>1">
                <img :src="myResumeDeleteIcon" />删除</span>
            </template>
          </div>
          <div class="displayBlock" v-if="!campusExpItem.editStatus">
            <!-- 非编辑状态 -->
            <div class="displayInfo">
              <span class="bold"> {{campusExpItem.person_studentLeaderjob}} </span>
              <span class="divide" v-if="campusExpItem.jtzw"></span>
              <span class="bold"> {{campusExpItem.jtzw}}</span>
              <span class="divide"
                v-if="campusExpItem.person_studentLeaderstartdatev2&&campusExpItem.person_studentLeaderstopdatev2"></span>
              <span> {{campusExpItem.person_studentLeaderstartdatev2}} -
                {{campusExpItem.person_studentLeaderstopdatev2}}</span>
            </div>
            <div class="summaryContainer">
              <div class="summary richContent" :ref="`richContent${index}`"
                v-html="campusExpItem.person_studentLeaderjobdesc"></div>
              <div class="expendContainer" @click="expandContent(campusExpItem,index)" v-if="campusExpItem.showExpend">
                <el-button type="text"
                  :icon="campusExpItem.expendButtonText=='展开'?'el-icon-arrow-down':'el-icon-arrow-up'">
                  {{campusExpItem.expendButtonText||"展开"}}
                </el-button>
              </div>
            </div>
          </div>
          <div class="editBlock" v-else :id="campusExpItem.editStatus?'edittingBlock':''">
            <!-- 编辑状态 -->
            <div class="resumeItemEdit">
              <el-form :ref="'campusEditForm'+campusExpItem.person_studengLeaderId" :model="campusExpItem.editForm"
                inline :rules="rules">
                <el-form-item label="校园组织名称" prop="leaderName">
                  <el-input v-model="campusExpItem.editForm.leaderName" placeholder="请输入校园组织名称"
                    maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="校园时间" prop="dateArr">
                  <el-date-picker v-model="campusExpItem.editForm.dateArr" size="small" type="monthrange" align="right"
                    unlink-panels value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始时间"
                    end-placeholder="结束时间">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="校园经验内容" prop="des" class="textAreaFormItem">
                  <el-input type="textarea" :rows="10" placeholder="请输入校园经验内容" v-model="campusExpItem.editForm.des"
                    :maxlength="1600" show-word-limit></el-input>
                </el-form-item>
                <el-form-item class="wholeLineFormItem">
                  <div class="submitButtons">
                    <el-button size="small" @click="handleCancelForItem(campusExpItem)">取 消</el-button>
                    <el-button size="small" type="primary" @click="handleSaveForItem(campusExpItem)">完 成</el-button>
                  </div>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import myResumeItemsMixin from "@/mixin/myResumeItemsMixin.js";
export default {
  name: "campusExperienceInfo",
  mixins: [myResumeItemsMixin],
  props: {
    resumeData: {
      type: Array,
      default: () => [],
    },
    editStatus: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      resumeDataList: [], //
      rules: {
        leaderName: [
          { required: true, message: "请输入校园组织名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        dateArr: [
          { required: true, message: "请选择校园时间", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    resumeData: {
      immediate: true,
      handler(newValue, oldValue) {
        //console.log("校园经历campusExperienceInfo基本信息更新：", newValue);
        this.resumeDataList = newValue.map((campusExpItem, index) => {
          if (!campusExpItem.hasOwnProperty("editStatus")) {
            this.$set(campusExpItem, "editStatus", false);
          }
          if (!campusExpItem.hasOwnProperty("editForm")) {
            this.$set(campusExpItem, "editForm", {
              dateArr: [
                campusExpItem.person_studentLeaderstartdatev2,
                campusExpItem.person_studentLeaderstopdatev2
                  ? campusExpItem.person_studentLeaderstopdatev2
                  : "",
              ],
              leaderName: campusExpItem.person_studentLeaderjob,
              des: campusExpItem.person_studentLeaderjobdesc,
              leaderId: campusExpItem.person_studengLeaderId,
            });
          }
          if (!campusExpItem.hasOwnProperty("showExpend")) {
            this.isOverflowing(campusExpItem, `richContent${index}`);
          }
          return campusExpItem;
        });
      },
    },
    editStatus: {
      handler(newValue, oldValue) {
        //如果新值是false要把所有的editStatus改为false
        if (newValue == false) {
          this.resumeDataList.forEach((item) => {
            item.editStatus = false;
            if (item.isNew) {
              this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1); // 新的删除
            }
          });
        }
      },
    },
  },
  methods: {
    //工作经历保存
    async handleSaveForItem(campusExpItem) {
      this.$refs[
        `campusEditForm` + campusExpItem.person_studengLeaderId
      ][0].validate((valid) => {
        if (valid) {
          const { dateArr, leaderName, des, leaderId } = campusExpItem.editForm;
          const [startDate, endDate] = dateArr;
          const finalParams = {
            op: "person_sub_busi",
            func: "updatePersonStudentLeader",
            version: "v5.9.18",
            forName: "iphone",
            param: {
              personId: this.personId,
              startDate,
              endDate,
              bToNow: 0,
              leaderName,
              des,
              leaderId,
            },
          };

          if (campusExpItem.isNew) {
            finalParams.func = "addPersonStudentLeader";
          }
          //console.log("修改校园经历参数：", finalParams);
          // return;
          this.resumeGeneralInterface(
            finalParams,
            () => {
              this.changeEditStatus(campusExpItem, false);
              campusExpItem.isNew = false;
            },
            () => {}
          );
        } else {
          //console.log("error submit!!");
          return;
        }
      });
    },
    handleDeletClick(campusExpItem) {
      this.$confirm("确定删除该条校园经历吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //下面才是删除方法
          const finalParams = {
            op: "person_sub_busi",
            func: "deletePersonStudentLeaderDetail",
            version: "v5.9.18",
            forName: "iphone",
            param: {
              array: {
                personId: this.personId,
                person_studengLeaderId: campusExpItem.person_studengLeaderId,
              },
            },
          };
          this.resumeGeneralInterface(
            finalParams,
            () => {},
            () => {}
          );
        })
        .catch(() => {});
    },
    handleCancelForItem(campusExpItem) {
      if (campusExpItem.isNew) {
        this.resumeDataList.splice(
          this.resumeDataList.indexOf(campusExpItem),
          1
        );
      } else {
        this.$set(campusExpItem, "editForm", {
          dateArr: [
            campusExpItem.person_studentLeaderstartdatev2,
            campusExpItem.person_studentLeaderstopdatev2,
          ],
          leaderName: campusExpItem.person_studentLeaderjob,
          des: campusExpItem.person_studentLeaderjobdesc,
          leaderId: campusExpItem.person_studengLeaderId,
        });
      }
      this.changeEditStatus(campusExpItem, false);
    },
  },
};
</script>

<style lang="scss" scoped>
.resumeItemContainer {
  display: flex;
  flex-direction: column;
  margin: 20px 0 16px;
  .resumeItemTitle {
    display: flex;
    align-items: center;
    .resumeItemTitleFlag {
      width: 4px;
      height: 19px;
      background: #0b6eff;
      border-radius: 2px;
      margin-right: 10px;
    }
    .resumeItemTitleContent {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      margin-right: 19px;
    }
    .resumeItemTitleAddIcon {
      display: flex;
      align-items: center;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #4880ff;
      cursor: pointer;
      .resumeItemAddIcon {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
  }
  .resumeItemMain {
    margin-top: 16px;
    .resumeItemDisplayList {
      .resumeItemDisplayItem {
        position: relative;
        margin-bottom: 16px;
        &:last-of-type {
          margin-bottom: 0px;
        }
        &:first-of-type {
          .displayBlock {
            .summaryContainer {
              .summary {
                margin-top: 0px;
              }
            }
          }
        }
        &:hover {
          .displayBlock {
            background: #f8f8f8;
            border-radius: 8px;
            padding: 16px 16px 16px 16px;
          }
          .resumeItemDisplayItemEditButtons {
            display: block;
          }
        }
        .resumeItemDisplayItemEditButtons {
          display: none;
          position: absolute;
          top: -18px;
          right: 0;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #4880ff;
          line-height: 18px;
          & > span {
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            margin-left: 23px;
            user-select: none;
            & > img {
              width: 16px;
              height: 16px;
              margin-right: 5px;
            }
          }
        }
        .displayBlock {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          // margin-right: 150px;
          color: #666666;
          line-height: 18px;
          text-align: left;
          font-style: normal;
          .displayInfo {
            display: flex;
            align-items: center;
          }
          .summary {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 14px;
            color: #888888;
            line-height: 21px;
            text-align: left;
            font-style: normal;
            margin-top: 9px;
            margin-bottom: 0px;
            &:first-of-type {
              margin-top: 0px;
            }
          }
          .bold {
            font-weight: bold;
            font-size: 15px;
            color: #000000;
            line-height: 21px;
          }
          .divide {
            width: 1px;
            height: 11px;
            background: #cccfd7;
            margin: 0 12px;
          }
        }
        .editBlock {
          margin-top: 2px;
          background: #f8f8f8;
          border-radius: 8px;
          padding: 16px;
          ::v-deep .el-form-item {
            display: inline-flex;
            flex-direction: column;
            min-width: 251px;
            width: calc(33% - 40px);
            margin-right: 24px;
            margin-bottom: 16px;
            margin-top: 0px;
            &.textAreaFormItem {
              width: 100%;
              margin-bottom: 14px;
              .el-textarea {
                textarea {
                }
              }
            }
            &.wholeLineFormItem {
              width: 100%;
              margin-bottom: 0px;
              .el-form-item__content {
                display: flex;
                justify-content: space-between;
                .submitButtons {
                  width: 100%;
                  display: flex;
                  justify-content: flex-end;
                  margin-top: 11px;
                  .el-button {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 100px;
                    height: 30px;
                    background: #ffffff;
                    border-radius: 4px;
                    border: 1px solid #cdcdcd;
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    text-align: left;
                    font-style: normal;
                    padding: 0;
                    &.el-button--primary {
                      width: 100px;
                      height: 30px;
                      background: #4880ff;
                      border-radius: 4px;
                    }
                  }
                }
                &::before {
                  display: none;
                }
                &::after {
                  display: none;
                }
              }
            }
            .el-form-item__label {
              height: 22px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 14px;
              color: #666660;
              line-height: 20px;
              text-align: left;
              font-style: normal;
            }
          }
          .submitButtons {
            display: flex;
            justify-content: flex-end;
            .el-button {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 100px;
              height: 30px;
              background: #ffffff;
              border-radius: 4px;
              border: 1px solid #cdcdcd;
              font-family: PingFang-SC, PingFang-SC;
              font-weight: bold;
              font-size: 14px;
              text-align: left;
              font-style: normal;
              padding: 0;
              &.el-button--primary {
                width: 100px;
                height: 30px;
                background: #4880ff;
                border-radius: 4px;
              }
            }
          }
        }
      }
    }
  }
  // ::v-deep .el-date-editor .el-range-input {
  //   background-color: #ffffff;
  // }
  ::v-deep .el-textarea {
    margin-top: 4px;
  }
  ::v-deep .el-select {
    .el-input__inner {
      width: 250px;
    }
  }
}
::v-deep .el-textarea .el-input__count {
  background-color: transparent;
  bottom: -33px;
}
::v-deep .el-textarea .el-input__count {
  background-color: transparent;
}
</style>